import Store from 'electron-store'
import uuid from 'uuid/v4'
const datastore = new Store({ 'name': 'ItemQuery' })
datastore.set('username', 'fei')
if (typeof (datastore.get('password')) === 'undefined') {
  datastore.set('password', '123')
}
// console.log(datastore.path)

export function saveItems (items) {
  datastore.set('items', items)
  return this
}

export function getData (key) {
  return datastore.get(key) || null
}
export function setData (key, value) {
  datastore.set(key, value)
}
export function getDBPath () {
  return datastore.path
}
export function addItems (items) {
  const itemsWithProps = items.map(item => {
    let nameString = item.name.toString().trim()
    let categoryString = item.category.toString().trim()
    let remarkString = item.remark.toString().trim()
    let priceString = item.price.toString().trim()
    let specificationString = item.specification.toString().trim()
    return {
      id: uuid(),
      name: nameString,
      category: categoryString,
      remark: remarkString,
      order: item.order.toString(),
      allInfo: nameString + specificationString + priceString + categoryString + remarkString,
      specification: specificationString,
      unit: item.unit.toString().trim(),
      price: priceString
    }
  })
  return saveItems(itemsWithProps)
}
export function clearItems () {
  datastore.delete('items')
}
// const Store = require('electron-store')
// class DataStore extends Store {
//   constructor (settings) {
//     super(settings)
//     this.items = this.get('items') || []
//     this.set('username', 'fei')
//     this.set('password', '123')
//   }
//   saveItems () {
//     this.set('items', this.items)
//     return this
//   }
//   getItems () {
//     return this.get('items') || []
//   }
//   addItems (items) {
//     const itemsWithProps = items.map(item => {
//       return {
//         id: uuid(),
//         name: item.name,
//         company: item.company,
//         remark: item.remark,
//         isFavor: item.isFavor,
//         charge: item.name.trim() + item.company.trim()
//       }
//     }).filter(item => {
//       const currentName = this.getItems().map(item => item.charge)
//       return currentName.indexOf(item.charge) < 0
//     })
//     this.items = [...this.items, ...itemsWithProps]
//     return this.saveItems()
//   }
//   getData (key) {
//     return this.get(key)
//   }
// }
// export default DataStore
